import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls 1.2  as Old
import QtQuick.Controls.Material 2.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.4
import  QtQml.Models 2.2
import  "Help.js"  as  Help
//Window作为最顶层元素 此时 他是我们所有子元素的父亲。
ApplicationWindow{
    //宽度 高度 为480*320 但是不用管它 因为在windows下 可以通过鼠标调整宽度大小
    width: 1024
    height: 768
    visible: true
    id:root


    //Model
    ListModel {
        id: objModel
        Component.onCompleted: {
        }
    }
    // 添加一个新分组 如果存在则刷新子列表的内容 不存在则新建 addFenzu("坑货",[{name:"1"},{name:"2"}])
    function  addFenzu(fenzuname,sub)
    {
        for(var x=0;x<objModel.count;x++)
        {
            if(objModel.get(x).fenzuname==fenzuname)
            {
                console.log("分组名字已经存在")
                //如果存在此分组 只刷新subNode
                 objModel.get(x).subNode = sub
                return
            }
        }

        objModel.append({"fenzuname":fenzuname,"subNode":sub})

    }

    //删除指定的分组 传入分组名 removeFenzu("A3")
    function removeFenzu(fenzuname)
    {
        for(var x=0;x<objModel.count;x++)
        {
            if(objModel.get(x).fenzuname==fenzuname)
            {
                objModel.remove(x,1)
                return
            }
        }
    }
//删除指定index的分组
    function removeFenzuByIndex(__index)
    {
          objModel.remove(__index,1)

    }



//添加单个好友 到指定列表    addSubNode("坑货",{name:"阿央",zaixian:"yes"})
    function  addSubNode(fenzuname,sub)
    {
        for(var x=0;x<objModel.count;x++)
        {
            if(objModel.get(x).fenzuname==fenzuname)
            {
                var subnode = objModel.get(x).subNode;
                for(var y=0;y<subnode.count;y++)
                {
                    if(subnode.get(y).name==sub.name)
                    {
                        console.log("防止重复添加")
                        subnode.get(y).name=sub.name;
                        subnode.get(y).zaixian=sub.zaixian

                        return
                    }
                }

                subnode.append(sub)
            }
        }


    }


    //Delegate
    Component {
        id: objRecursiveDelegate
        Rectangle{

            width: parent.width
            height: subview.height+titiles.height
            Rectangle{
                id:titiles
                width: parent.width
                height: 60
                border.color: "#cccccc"
                Item{
                    id:anctionItem
                    visible: subModelList.count === 0 ? 1:0
                    Action{id:actionPanel}
                }

                Text {
                    id: febzu
                    visible: subModelList.count === 0 ? 0:1
                    text:"分组:"+ model.fenzuname
                    anchors.centerIn: parent
                }

                BorderImage {
                    id: bg
                    source: "qrc:/+.png"
                    width: 24; height: 24
                    border.left: 5; border.top: 5
                    border.right: 5; border.bottom: 5
                    anchors.left: parent.left
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        subview.visible = !subview.visible
                        if(subview.visible==true)
                        {
                            bg.source="qrc:/-.png"
                        }else{
                            bg.source="qrc:/+.png"
                        }
                    }
                }
            }

            ListView{
                id:subview
                anchors.top: titiles.bottom
                width: parent.width
                visible: false
                height: visible==true?contentHeight:0
                model:subNode
                delegate:Rectangle{
                    width: parent.width
                    height: 72
                     border.color: "#cccccc"
                    Text {
                        text: model.name
                        anchors.centerIn: parent
                    }
                }
            }
        }

    }

    //View
    ListView{
        id:mainlist
        width: parent.width
        height: parent.height - 60
        model:objModel
        delegate: objRecursiveDelegate
    }

    //添加分组
    Button{
        anchors.top: mainlist.bottom
        height: 60
        width: 100
        text: "添加分组"
        onClicked: {
            removeFenzu("A3")
            addFenzu("坑货",[{name:"1"},{name:"2"}])
            addFenzu("ABC",[{name:"3"},{name:"4"}])
            addSubNode("坑货",{name:"阿央",zaixian:"no"})
            addSubNode("坑货",{name:"阿央",zaixian:"yes"})
            addSubNode("坑货",{name:"阿央2"})
        }

    }


}

